﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace 每日时间作息
{
    /// <summary>
    /// Window_LockScreen.xaml 的交互逻辑
    /// </summary>
    public partial class Window_LockScreen : Window
    {

        public DispatcherTimer dispatcherTimer = new DispatcherTimer();

        MainWindow MainWindow;

        public bool ProperClose = false;

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Alt && e.SystemKey == Key.F4 ||
                Keyboard.Modifiers == ModifierKeys.Control && e.SystemKey == Key.Escape)
            {
                e.Handled = true;
            }
            else if (Keyboard.Modifiers == ModifierKeys.Alt && e.SystemKey == Key.Tab)
            {
                e.Handled = true;
            }
            else
            {
                base.OnKeyDown(e);
            }
        }




        public Window_LockScreen()
        {
            InitializeComponent();

            MainWindow = App.Current.MainWindow as MainWindow;

            TextBlock.Text = $"00:{Properties.Settings.Default.LockInterval.ToString().PadLeft(2, '0')}:00";

            //TextBlock.Text = $"00:00:05";
            DateTime now = DateTime.Now;
            nowTextBlock.Text = now.ToString("yyyy/MM/dd HH:mm:ss ") + GetWeek(now);

            VisualBrush brush = new VisualBrush();
            brush.Visual = MainGrid;
            //brush.Stretch = Stretch.Uniform;
            border.Background = brush;
            //border.Effect = new BlurEffect()
            //{
            //    Radius = 100,
            //    RenderingBias = RenderingBias.Performance
            //};
            //border.Margin = new Thickness(-this.Margin.Left, -this.Margin.Top, 0, 0);

            dispatcherTimer.Interval = TimeSpan.FromSeconds(1);
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);

            FadeIn();
        }

        private string GetWeek(DateTime dateTime)
        {
            string week = string.Empty;
            switch ((int)dateTime.DayOfWeek)
            {
                case 0:
                    week = "星期日";
                    break;
                case 1:
                    week = "星期一";
                    break;
                case 2:
                    week = "星期二";
                    break;
                case 3:
                    week = "星期三";
                    break;
                case 4:
                    week = "星期四";
                    break;
                case 5:
                    week = "星期五";
                    break;
                default:
                    week = "星期六";
                    break;
            }
            return week;
        }

        public async void FadeIn()
        {
            this.Opacity = 0;
            double opacity = this.Opacity;
            await Task.Run(() =>
            {
                while (opacity < 1)
                {
                    this.Dispatcher.Invoke((Action)delegate { this.Opacity += 0.02; opacity = this.Opacity; });
                    Task.Delay(1).Wait();
                }
            });
        }

        public async void FadeOut()
        {
            this.Opacity = 1;
            double opacity = this.Opacity;
            await Task.Run(() =>
            {
                while (opacity > 0)
                {
                    this.Dispatcher.Invoke((Action)delegate { this.Opacity -= 0.1; opacity = this.Opacity; });
                    Task.Delay(1).Wait();
                }
            });
            this.Close();
        }

        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;
            nowTextBlock.Text = now.ToString("yyyy/MM/dd HH:mm:ss ") + GetWeek(now);


            int hour = int.Parse(TextBlock.Text.Split(':').First());
            int minute = int.Parse(TextBlock.Text.Split(':')[1]);
            int second = int.Parse(TextBlock.Text.Split(':').Last());


            TimeSpan timeSpan = new TimeSpan(hour, minute, second);
            timeSpan = timeSpan - new TimeSpan(0, 0, 1);
            if (timeSpan == new TimeSpan(0, 0, 0))
            {
                dispatcherTimer.Stop();
                TextBlock.Text = $"00:{Properties.Settings.Default.LockInterval.ToString().PadLeft(2, '0')}:00";
                //TextBlock.Text = $"00:00:05";
                if (MainWindow != null)
                {
                    MainWindow.TextBlock.Text = $"00:{Properties.Settings.Default.HintInterval.ToString().PadLeft(2, '0')}:00";
                    MainWindow.dispatcherTimer.Stop();
                }
                ProperClose = true;
                FadeOut();
            }



            TextBlock.Text = $"{timeSpan.Hours.ToString().PadLeft(2, '0')}:{timeSpan.Minutes.ToString().PadLeft(2, '0')}:{timeSpan.Seconds.ToString().PadLeft(2, '0')}";


            //设置 mainwindow
            if (MainWindow != null)
            {
                if (MainWindow.Shutdown)
                {
                    ProperClose = true;
                    dispatcherTimer.Stop();
                    TextBlock.Text = $"00:{Properties.Settings.Default.LockInterval.ToString().PadLeft(2, '0')}:00";
                    //TextBlock.Text = $"00:00:05";
                    FadeOut();
                }
            }




        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            //if (e.Key == Key.Escape) this.Close();
        }

        private void Window_ContentRendered(object sender, EventArgs e)
        {
            dispatcherTimer.Start();
        }

        private void Window_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                this.DragMove();
            }
        }


        public bool Fading = false;


        private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Window_Setttings window_Setttings = 每日时间作息.GetWindow.Get("Window_Setttings") as Window_Setttings;
            if (window_Setttings != null) window_Setttings.Close();
            window_Setttings = new Window_Setttings(this);
            window_Setttings.ShowDialog();
        }



        private void Window_Closed(object sender, EventArgs e)
        {
            if (Properties.Settings.Default.Info == "") Properties.Settings.Default.Info = "填写锁屏信息";
            Properties.Settings.Default.Save();
        }

        private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            ClearFocusTB.Focus();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!ProperClose) e.Cancel = true;
        }

    }
}
